package paper.tags;

import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.util.TreeMap;
import java.util.Iterator;
import java.sql.*;

import paper.db.DB;
import paper.tags.Tag;

/**
 *  Represents a collection of tags. 
 */

public class TagBag
{
	public Vector<Tag> tagList;
	public int howManyTags;
	public Hashtable<String,Integer> tagtable;
	public Hashtable<Integer,String> reversetagtable;

	public TagBag(){
        tagtable=new Hashtable<String,Integer>();
        reversetagtable=new Hashtable<Integer,String>();
        tagList = new Vector<Tag>();
        howManyTags=0;
	}

	public TagBag(Vector someTags){
		// this needs cacheing, this hash table stuff
		Tag tempTag;
		tagtable=new Hashtable<String,Integer>();
		howManyTags=0;
		int test=0;
		reversetagtable=new Hashtable<Integer,String>();
		for(Enumeration<Tag> e=someTags.elements(); e.hasMoreElements();){
			tempTag=e.nextElement();
			howManyTags++;
			if(tagtable.containsKey(tempTag.getTag())){
				test=tagtable.get(tempTag.getTag());
				test++;
				tagtable.put(tempTag.getTag(),test);
			} else {
				tagtable.put(tempTag.getTag(),1);
			}
		}
		 /*             //Gets keys from hashtable
		Enumeration enu = tagtable.keys();
		String myString="";
		while (enu.hasMoreElements()){
			myString=(String)enu.nextElement();
			reversetagtable.put((Integer)tagtable.get(myString),myString);
		  } 	
		System.out.println(tagtable.toString());
		System.out.println(reversetagtable.toString());
		*/
		tagList=someTags;

	}
	public int getSize(){
		return howManyTags;
	}
	
   public String tagControls(String key){
        String returnme="";
	returnme="<a href='/tag?id="+key+"'>[tags&nbsp;+]</a>";
        return returnme;
        }

	public String getTagCloud(){
		// TODO: Add tag cloud cache. Should generate INFREQUENTLY	
		// what tags appear more than once
		String returnVar="";
		int maxvalue=0;
		int minvalue=1;
		int howmanymax=0;
		Vector output=new Vector();
		Enumeration enu = (Enumeration)tagtable.elements();
                int myInt=0;
		int limit=22;
		String foo;	

		if(howmanymax<limit) {
			limit=howmanymax;
		}
		int i=limit;
                while (enu.hasMoreElements()){
                        myInt=(Integer)enu.nextElement();
			howmanymax++;
			if(myInt>maxvalue){
				maxvalue=myInt;
			}
                  }
	
		for(int blah=maxvalue;blah>0;blah--){
			enu=tagtable.keys();
			while(enu.hasMoreElements()){
				foo=(String)enu.nextElement();
				if((Integer)tagtable.get(foo)==blah){
					limit--;
				}
			}
			if(blah==0){
				minvalue=blah;
				break;
			}
			if(limit==0){
				minvalue=blah;
				break;
			}
		}


		int maxbound=6;
		int minbound=1;
		limit=22;
		if(minvalue<1) minvalue=1;
		for(int blah=maxvalue;blah>0;blah--){
			enu=tagtable.keys();
			while(enu.hasMoreElements()){
				foo=(String)enu.nextElement();
				if((Integer)tagtable.get(foo)==blah){
					limit--;
					int result=(int)Math.ceil(((maxbound-minbound)/(double)(maxvalue/minvalue)*blah)+(minbound*maxvalue-maxbound*minvalue)/(double)(maxvalue-minvalue));
					output.addElement(" <span class='tag"+result+"'>&nbsp;"+foo+"&nbsp;("+blah+") </span> ");			


				}
				if(limit<0){
					break;
				}
			}
			if(blah==0){
				break;
			}
			if(limit<0){
				break;
			}

		}

		// font weights - the graduation between max and min	
		for(Enumeration e=output.elements(); e.hasMoreElements();){
			returnVar=returnVar+e.nextElement();
		}
		return returnVar;
	}	

	public String getAll(String prefix,String id, String userid){
		String returnVar="";
		String suffix="";
		Tag tempTag;
		int temp=0;
		for(Enumeration e=tagList.elements();e.hasMoreElements();){
			tempTag=(Tag)e.nextElement();
			if(tempTag.getUserID() .equals(userid)){
				suffix=" <a href='"+prefix+"?deletetagid="+tempTag.getTagID()+"&amp;id="+id+"'>-</a>";
			}
			if (temp>0){
				returnVar=returnVar+", "+tempTag.getTag()+suffix;
			} else {
				returnVar=tempTag.getTag()+suffix;
			}
			temp++;
		}
		return returnVar;
	}

	public String getAll(){
		String returnVar="";
		Tag tempTag;
		int temp=0;
		for(Enumeration e=tagList.elements();e.hasMoreElements();){
			tempTag=(Tag)e.nextElement();
			if (temp>0){
				returnVar=returnVar+", "+tempTag.getTag();
			} else {
				returnVar=tempTag.getTag();
			}
			temp++;
		}
		return returnVar;
	}

	public String getPreview(){
		String returnVar="";
		Tag tempTag;
		int temp=0;
        
        if (tagList == null) return "";
	
		for(Enumeration e=tagList.elements();e.hasMoreElements();){
			if(temp>2){
				break;
			}
			tempTag=(Tag)e.nextElement();
			if (temp>0){
				returnVar=returnVar+", "+tempTag.getTag();
			} else {
				returnVar=tempTag.getTag();
			}
			temp++;
		}
		return returnVar;
	}
	
	public Vector getMostPopular(){
		// work out and return the most popular tags
		return tagList;
	}

	public Vector getTaglist(){
		return tagList;
	}		

	
	public void addFromResultSet(ResultSet rs) throws SQLException {
        if (rs.getString("tag") != null) {
            Tag newTag=new Tag(rs.getString("tag"), rs.getString("tagid"),
                               rs.getString("paperkey"), rs.getString("userid"));
            tagList.add(newTag);
            howManyTags++;
            Integer test=tagtable.get(newTag.getTag());
            if(test == null) {
                test = 0;
            }
            tagtable.put(newTag.getTag(),test+1);
        }
	}
	

}

/* Modeline for ViM {{{
 * vim: set ts=4:
 * vim600: fdm=marker fdl=0 fdc=3:
 * }}} */

